package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenlida
 * @date 2018/12/20 15:16
 * @description
 */
public class Code230 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.right = new TreeNode(4);
        TreeNode r1 = new TreeNode(1);
        root.left = r1;
        r1.right = new TreeNode(2);
        new Code230().kthSmallest(root, 1);
    }

    public int kthSmallest(TreeNode root, int k) {
        List<Integer> result = new ArrayList<>();
        getResult(root, result);
        return result.stream().distinct().sorted().collect(Collectors.toList()).get(k - 1);
    }

    private void getResult(TreeNode root, List<Integer> result) {
        if (root == null) { return; }
        result.add(root.val);
        getResult(root.left, result);
        getResult(root.right, result);

    }
}
